• Author: Hatem Bou Hjar
  • Date: 30/03/2022
  • Ennonce
  1. Installer une base de données Mongodb
  2. Iinsérer les données du fichier joint (tu peux t'aider du code ci-dessus).
  3. Répondre aux exercices suivants en écrivant le code python :
    a. Compter le nombre de femmes / d'hommes.
    b. Écrire une fonction qui renvoie les entreprises de plus de N personnes.
    c. Écrire une fonction qui prend en paramètre un métier et qui renvoie la pyramide des âges pour ce métier."

Importation des bibliotheques¶

In [13]:
import json 
from pymongo import MongoClient
from dateutil.relativedelta import relativedelta
from datetime import datetime
from datetime import date
import plotly.graph_objects as gp

Fonction collection¶

In [3]:
def collection(uri):
    client = MongoClient(uri)
    database = client["rhobs"]
    collection = database["people"]
    return collection 

Fonction load¶

In [4]:
def load (uri, datapath):
    coll = collection(uri = uri)
    with open(datapath,"r") as fp:
        data = json.load(fp)
        
        for person in data:
            coll.insert_one(person)        

Integration des donnees¶

In [5]:
load(uri="mongodb://localhost:27017/", datapath="C:/Users/hatem/OneDrive/Desktop/Entretient stage rhodes/data.json")

Affichage du nombre des femmes¶

In [6]:
nb_femmes = collection("mongodb://localhost:27017/").count_documents({"sex": "F"})
print("Nombre de femmes :", nb_femmes)
Nombre de femmes : 25042

Affichage du nombre des hommes¶

In [7]:
nb_hommes = collection("mongodb://localhost:27017/").count_documents({"sex": "M"})
print("Nombre de hommes :", nb_hommes)
Nombre de hommes : 24958

Connection a la base des donnees¶

In [8]:
mycollection = collection("mongodb://localhost:27017/")

Fonction trouver_entreprise¶

In [9]:
def trouver_entreprise():  

    while True:
        try:
            nb_personne = int(input("Entrez le nombre de personne: "))
            break

        except ValueError:
            print("Entrez seulement des entiers")
            
    big_companies = mycollection.aggregate([
        {"$group": {"_id": "$company", "count": {"$sum": 1}}},
        {"$match": {"count": {"$gte": nb_personne}}}
    ])
    resultat = list(big_companies)
    if  not resultat:
        print("Pas de company avec ce nombre de personne")
    else:
        return ["entrprise: " + company["_id"] for company in resultat]
    
trouver_entreprise()
Entrez le nombre de personne: 15
Out[9]:
['entrprise: Becker',
 'entrprise: Barbe',
 'entrprise: Foucher',
 'entrprise: Adam',
 'entrprise: Lenoir',
 'entrprise: Delmas',
 'entrprise: Marchand',
 'entrprise: Gallet',
 'entrprise: Chauvin',
 'entrprise: Léger',
 'entrprise: Garnier',
 'entrprise: Le Roux',
 'entrprise: Maury',
 'entrprise: Menard',
 'entrprise: Lemaire',
 'entrprise: Bruneau',
 'entrprise: Ruiz',
 'entrprise: Guilbert',
 'entrprise: Michel',
 'entrprise: Weber',
 'entrprise: Aubert',
 'entrprise: Didier',
 'entrprise: Payet',
 'entrprise: Brunet',
 'entrprise: Fabre',
 'entrprise: Wagner',
 'entrprise: Rousset',
 'entrprise: Daniel',
 'entrprise: Bousquet',
 'entrprise: Masson',
 'entrprise: Jacques',
 'entrprise: Denis',
 'entrprise: Gosselin',
 'entrprise: Camus',
 'entrprise: Ferrand',
 'entrprise: Carpentier',
 'entrprise: Ribeiro',
 'entrprise: Rodriguez',
 'entrprise: Barbier',
 'entrprise: Maillard',
 'entrprise: Diallo',
 'entrprise: Lagarde',
 'entrprise: Bailly',
 'entrprise: Hernandez',
 'entrprise: Rousseau',
 'entrprise: Hamel',
 'entrprise: Simon',
 'entrprise: Da Silva',
 'entrprise: Blanchet',
 'entrprise: Toussaint',
 'entrprise: Letellier',
 'entrprise: Delorme',
 'entrprise: Ferreira',
 'entrprise: Bernard',
 'entrprise: Muller',
 'entrprise: Maillot',
 'entrprise: Henry',
 'entrprise: Giraud',
 'entrprise: Gillet',
 'entrprise: Delahaye',
 'entrprise: Bouvet',
 'entrprise: Durand',
 'entrprise: Samson',
 'entrprise: Duval',
 'entrprise: Guibert',
 'entrprise: Blot',
 'entrprise: Morel',
 'entrprise: Monnier',
 'entrprise: Leleu',
 'entrprise: Couturier',
 'entrprise: Pascal',
 'entrprise: Boulay',
 'entrprise: Lemaître',
 'entrprise: Alves',
 'entrprise: Faure',
 'entrprise: Weiss',
 'entrprise: Guyot',
 'entrprise: Poirier',
 'entrprise: Dufour',
 'entrprise: Goncalves',
 'entrprise: Brunel',
 'entrprise: Laroche',
 'entrprise: Rossi',
 'entrprise: Morin',
 'entrprise: Reynaud',
 'entrprise: Tanguy',
 'entrprise: Roche',
 'entrprise: Voisin',
 'entrprise: Lemonnier',
 'entrprise: Hoareau',
 'entrprise: Dupont',
 'entrprise: Pages',
 'entrprise: Laporte',
 'entrprise: Marques',
 'entrprise: Bertrand',
 'entrprise: Bernier',
 'entrprise: De Oliveira',
 'entrprise: Auger',
 'entrprise: Mace',
 'entrprise: Caron',
 'entrprise: Perret',
 'entrprise: Alexandre',
 'entrprise: Charles',
 'entrprise: Chauveau',
 'entrprise: Pichon',
 'entrprise: Masse',
 'entrprise: Baron',
 'entrprise: Jacquot',
 'entrprise: Charpentier',
 'entrprise: Lamy',
 'entrprise: Guillaume',
 'entrprise: Gaillard',
 'entrprise: Normand',
 'entrprise: Philippe',
 'entrprise: Jean',
 'entrprise: Faivre',
 'entrprise: Grégoire',
 'entrprise: Teixeira',
 'entrprise: Lévêque',
 'entrprise: Petitjean',
 'entrprise: Lemoine',
 'entrprise: Vallée',
 'entrprise: Girard',
 'entrprise: Martin',
 'entrprise: Germain',
 'entrprise: Moreau',
 'entrprise: Georges',
 'entrprise: Millet',
 'entrprise: Lelièvre',
 'entrprise: Meunier',
 'entrprise: Le Gall',
 'entrprise: Rolland',
 'entrprise: Dumont',
 'entrprise: Pons',
 'entrprise: Nicolas',
 'entrprise: Seguin',
 'entrprise: Vaillant',
 'entrprise: Hubert',
 'entrprise: Martel',
 'entrprise: Pasquier',
 'entrprise: Mahe',
 'entrprise: Gonzalez',
 'entrprise: Renaud',
 'entrprise: Grenier',
 'entrprise: Aubry',
 'entrprise: Chartier',
 'entrprise: Besson',
 'entrprise: Dias',
 'entrprise: Besnard',
 'entrprise: Pierre',
 'entrprise: Leroy',
 'entrprise: Parent',
 'entrprise: Gautier',
 'entrprise: Neveu',
 'entrprise: Perez',
 'entrprise: Sanchez',
 'entrprise: Evrard',
 'entrprise: Lecomte',
 'entrprise: Ramos',
 'entrprise: Prévost',
 'entrprise: Fischer',
 'entrprise: Antoine',
 'entrprise: Klein',
 'entrprise: Marin',
 'entrprise: Pelletier',
 'entrprise: Rémy',
 'entrprise: Loiseau',
 'entrprise: Thibault',
 'entrprise: Bodin',
 'entrprise: Merle',
 'entrprise: Blanc',
 'entrprise: Techer',
 'entrprise: Dupré',
 'entrprise: Richard',
 'entrprise: Berthelot',
 'entrprise: Jacquet',
 'entrprise: Riou',
 'entrprise: Dupuy',
 'entrprise: Jacob',
 'entrprise: Pruvost',
 'entrprise: Clerc',
 'entrprise: Robin',
 'entrprise: Marion',
 'entrprise: Vidal',
 'entrprise: Bazin',
 'entrprise: Bègue',
 'entrprise: Leroux',
 'entrprise: Munoz',
 'entrprise: Mallet',
 'entrprise: Mercier',
 'entrprise: Albert',
 'entrprise: Lefebvre',
 'entrprise: Gauthier',
 'entrprise: Morvan',
 'entrprise: Petit',
 'entrprise: Marie',
 'entrprise: Fontaine',
 'entrprise: Blanchard',
 'entrprise: Vasseur',
 'entrprise: Vincent',
 'entrprise: Andre',
 'entrprise: Roussel',
 'entrprise: Joly',
 'entrprise: Delattre',
 'entrprise: Schneider',
 'entrprise: Delannoy',
 'entrprise: Mendès',
 'entrprise: Perrot',
 'entrprise: Benoit',
 'entrprise: Costa',
 'entrprise: Traore',
 'entrprise: Peltier',
 'entrprise: Regnier',
 'entrprise: Raymond',
 'entrprise: Roy',
 'entrprise: Lopez',
 'entrprise: Colas',
 'entrprise: Perrier',
 'entrprise: Lévy',
 'entrprise: Pinto',
 'entrprise: Cordier',
 'entrprise: Boyer',
 'entrprise: Perrin',
 'entrprise: Martinez',
 'entrprise: Lesage',
 'entrprise: Gimenez',
 'entrprise: Dubois',
 'entrprise: Hoarau',
 'entrprise: Mary',
 'entrprise: Leconte',
 'entrprise: Lambert',
 'entrprise: Potier',
 'entrprise: Lacombe',
 'entrprise: Picard',
 'entrprise: Olivier',
 'entrprise: Leduc',
 'entrprise: Ledoux',
 'entrprise: Joseph',
 'entrprise: Fournier',
 'entrprise: Fernandes',
 'entrprise: Huet',
 'entrprise: Gilbert',
 'entrprise: Roger',
 'entrprise: Maurice',
 'entrprise: Lecoq',
 'entrprise: Berger',
 'entrprise: Sauvage',
 'entrprise: Lucas',
 'entrprise: Pineau',
 'entrprise: Blin',
 'entrprise: Launay',
 'entrprise: Dos Santos',
 'entrprise: Moulin',
 'entrprise: Coste',
 'entrprise: Martineau',
 'entrprise: Descamps',
 'entrprise: Benard',
 'entrprise: Guyon',
 'entrprise: Gaudin',
 'entrprise: Barre',
 'entrprise: Valette',
 'entrprise: Imbert',
 'entrprise: Buisson',
 'entrprise: Guillet',
 'entrprise: David',
 'entrprise: Poulain',
 'entrprise: Chrétien',
 'entrprise: Bonnin',
 'entrprise: Noël',
 'entrprise: Legros',
 'entrprise: Guillon',
 'entrprise: Maillet',
 'entrprise: Laine',
 'entrprise: Maréchal',
 'entrprise: Lopes',
 'entrprise: Louis',
 'entrprise: Da Costa',
 'entrprise: Valentin',
 'entrprise: Dumas',
 'entrprise: Gomez',
 'entrprise: Colin',
 'entrprise: Allard',
 'entrprise: Chauvet',
 'entrprise: Raynaud',
 'entrprise: Lebreton',
 'entrprise: Fleury',
 'entrprise: Coulon',
 'entrprise: Devaux',
 'entrprise: Pires',
 'entrprise: Grondin',
 'entrprise: Salmon',
 'entrprise: Roux',
 'entrprise: Hoarau et Fils',
 'entrprise: Meyer',
 'entrprise: Gilles',
 'entrprise: Arnaud',
 'entrprise: Rodrigues',
 'entrprise: Diaz',
 'entrprise: Bonnet',
 'entrprise: Thierry',
 'entrprise: Peron',
 'entrprise: Lacroix',
 'entrprise: Torres',
 'entrprise: Carre',
 'entrprise: Cohen',
 'entrprise: Dupuis',
 'entrprise: Martins',
 'entrprise: Humbert',
 'entrprise: Duhamel',
 'entrprise: Barthelemy',
 'entrprise: Lombard',
 'entrprise: Royer',
 'entrprise: Boulanger',
 'entrprise: Navarro',
 'entrprise: Étienne',
 'entrprise: Hardy',
 'entrprise: Ollivier',
 'entrprise: Bigot',
 'entrprise: Fernandez',
 'entrprise: Bertin',
 'entrprise: Chevalier',
 'entrprise: Garcia',
 'entrprise: Briand',
 'entrprise: Langlois',
 'entrprise: Clément',
 'entrprise: Guichard',
 'entrprise: Gérard',
 'entrprise: Lejeune',
 'entrprise: Marty',
 'entrprise: Rocher',
 'entrprise: Guillou',
 'entrprise: Carlier',
 'entrprise: Collet',
 'entrprise: Pottier',
 'entrprise: Breton',
 'entrprise: Verdier',
 'entrprise: Leclercq',
 'entrprise: Gomes',
 'entrprise: Joubert',
 'entrprise: Courtois',
 'entrprise: Deschamps',
 'entrprise: Dijoux',
 'entrprise: Leclerc',
 'entrprise: Godard',
 'entrprise: Guérin',
 'entrprise: Guillot',
 'entrprise: Paris',
 'entrprise: Thomas',
 'entrprise: Marchal',
 'entrprise: Chevallier',
 'entrprise: Hamon',
 'entrprise: Fouquet',
 'entrprise: Legendre',
 'entrprise: Legrand',
 'entrprise: Robert',
 'entrprise: Vallet',
 'entrprise: Lefèvre',
 'entrprise: Rivière',
 'entrprise: Bouvier',
 'entrprise: Leblanc',
 'entrprise: Cousin',
 'entrprise: Lebrun',
 'entrprise: Labbé',
 'entrprise: Boucher',
 'entrprise: Collin',
 'entrprise: De Sousa',
 'entrprise: Texier',
 'entrprise: Gros',
 'entrprise: Bouchet',
 'entrprise: Brun',
 'entrprise: Lebon',
 'entrprise: Jourdan',
 'entrprise: Mathieu',
 'entrprise: Rey',
 'entrprise: Renard',
 'entrprise: Allain',
 'entrprise: Bourgeois',
 'entrprise: Michaud',
 'entrprise: Baudry',
 'entrprise: Bourdon',
 'entrprise: Paul',
 'entrprise: Moreno',
 'entrprise: Delaunay',
 'entrprise: Hervé',
 'entrprise: Lefort',
 'entrprise: Tessier',
 'entrprise: Turpin',
 'entrprise: Bonneau',
 'entrprise: Charrier',
 'entrprise: Julien',
 'entrprise: Blondel',
 'entrprise: Le Goff',
 'entrprise: Nguyen',
 'entrprise: Laurent',
 'entrprise: Gay',
 'entrprise: Pereira',
 'entrprise: Hebert',
 'entrprise: Renault',
 'entrprise: François',
 'entrprise: Schmitt',
 'entrprise: Boutin']

Fonction pyramide_age¶

In [14]:
def pyramide_age(travail):
    
    entreprise = mycollection.find({"job": travail})

    male_ages = []
    female_ages = []
    for person in entreprise:
        birthdate = date.fromisoformat(person["birthdate"])
        age = relativedelta(date.today(), birthdate).years
        if person["sex"] == "M":
            male_ages.append(age)
        elif person["sex"] == "F":
            female_ages.append(age)

    x_M = [-age for age in male_ages]
    x_F = female_ages
    y_age = [f"{i*10}-{(i+1)*10-1}" for i in range(10)]

    fig = gp.Figure()

    fig.add_trace(gp.Bar(y=y_age, x=x_M, 
                         name='Male', 
                         orientation='h'))

    fig.add_trace(gp.Bar(y=y_age, x=x_F,
                         name='Female', 
                         orientation='h'))

    fig.update_layout(title=f"Pyramide age {travail}",
                      title_font_size=22, 
                      barmode='relative',
                      bargap=0.0, 
                      bargroupgap=0,
                      xaxis=dict(tickvals=[-80, -60, -40, -20, 0, 20, 40, 60, 80],
                                 ticktext=['80+', '70-79', '60-69', '50-59', '40-49', '30-39', '20-29', '10-19', '0-9'],
                                 title='Age',
                                 title_font_size=14),
                      yaxis=dict(title='Number of people',
                                 title_font_size=14)
                      )

    fig.show()
In [15]:
pyramide_age("aérodynamicien")